//
// Created by zhupi on 2024/11/2.
//

#ifndef DEMO2_EDIT_LIBRARY_DIALOG_H
#define DEMO2_EDIT_LIBRARY_DIALOG_H

#include <QDialog>
#include <QTabWidget>
#include <QStringList>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QStringListModel>
#include <QComboBox>
#include <QGroupBox>
#include "base_table_widget.h"
#include <QCompleter>

class EditLibraryDialog : public QDialog
{
    Q_OBJECT
public:
    explicit EditLibraryDialog(QWidget *parent);
    void Initial();


    void loadTableData(const std::vector<std::vector<QString>> &data) {
        tableWidget_->setRowCount(data.size());
        if (data.size() > 0) {
            tableWidget_->setColumnCount(data[0].size());
        }

        for (int i = 0; i < data.size(); ++i) {
            for (int j = 0; j < data[i].size(); ++j) {
                QTableWidgetItem *item = new QTableWidgetItem(data[i][j]);
                item->setFlags(item->flags() & ~Qt::ItemIsEditable);  // 禁用编辑
                tableWidget_->setItem(i, j, item);
            }
        }
        // 更新completer的选项
        updateCompleterModel();

//        tableWidget_->setRowCount(data.size());
//        if (data.size() > 0) {
//            tableWidget_->setColumnCount(data[0].size());
//        }
//
//        for (int i = 0; i < data.size(); ++i) {
//            for (int j = 0; j < data[i].size(); ++j) {
//                QTableWidgetItem *item = new QTableWidgetItem(data[i][j]);
//                item->setFlags(item->flags() & ~Qt::ItemIsEditable);  // 禁用编辑
//                tableWidget_->setItem(i, j, item);
//            }
//        }
//
//        // 更新completer的选项
//        updateCompleterModel();
    }

signals:
    void OKSg();
    void CancelSg();
    void RowSelectedSg(const QString &buttonName, const std::vector<QString> &rowData);

private:
    void InitTop();
    void InitMaterial();
    // 初始化搜索
    void InitSearch(QGroupBox *groupBox);
    //
    void SetTableWidget();
    void InitTableButton(QVBoxLayout *mainVbox);
    void InitButtom(QVBoxLayout *mainVbox);


    void ItemClicked(QTableWidgetItem *item)
    {
        int row = item->row();
        std::vector<QString> rowData;
        for (int col = 0; col < tableWidget_->columnCount(); ++col) {
            rowData.push_back(tableWidget_->item(row, col)->text());
        }
//        emit rowSelected(rowData);
    }

    // 标题栏点击
    void HeaderClicked(int column)
    {
        static bool ascending = true;
        tableWidget_->sortByColumn(column, ascending ? Qt::AscendingOrder : Qt::DescendingOrder);
        ascending = !ascending;  // 切换排序顺序
    }

    // 得到选择的行
    void GetSelectRow(std::vector< QString> &data);

private slots:
    void onSearchTextChanged(const QString &text) {
        int searchColumn = columnSelector->currentIndex();  // 获取选择的列
        for (int row = 0; row < tableWidget_->rowCount(); ++row) {
            QTableWidgetItem *item = tableWidget_->item(row, searchColumn);
            if (item && item->text().contains(text, Qt::CaseInsensitive)) {
                tableWidget_->setCurrentCell(row, searchColumn);  // 选中第一个匹配项
                return;
            }
        }
    }

    void onCompleterActivated(const QString &text) {
        int searchColumn = columnSelector->currentIndex();
        for (int row = 0; row < tableWidget_->rowCount(); ++row) {
            QTableWidgetItem *item = tableWidget_->item(row, searchColumn);
            if (item && item->text() == text) {
                tableWidget_->setCurrentCell(row, searchColumn);  // 选中匹配项
                return;
            }
        }
    }

private:
    QTabWidget *tabWidget_ {nullptr};
    QTableWidget *tableWidget_ {nullptr};

    // test
    void updateCompleterModel() {
        int searchColumn = columnSelector->currentIndex();
        QStringList itemList;
        for (int row = 0; row < tableWidget_->rowCount(); ++row) {
            QTableWidgetItem *item = tableWidget_->item(row, searchColumn);
            if (item) {
                itemList << item->text();
            }
        }
        itemList.removeDuplicates();  // 去重
        QStringListModel *model = new QStringListModel(itemList, completer);
        completer->setModel(model);
    }

    QLineEdit *searchBox;
    QCompleter *completer;
    QComboBox *columnSelector;
};


#endif //DEMO2_EDIT_LIBRARY_DIALOG_H
